﻿package cn.edu.zjusc.Webservice;

import java.io.File;

import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.jboss.wsf.spi.annotation.WebContext;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import cn.edu.zjusc.POJO.ConfigInfo;
import cn.edu.zjusc.POJO.SystemNoticeInfo;
import cn.edu.zjusc.Security.ProfileInterceptor;
import cn.edu.zjusc.Security.UserCave;

@Stateless
@WebService(name="SystemNoticeInfoService",serviceName="SystemNoticeInfoService")
@SOAPBinding(style=SOAPBinding.Style.RPC)
@WebContext(contextRoot="/ZJUSCIX/Service", urlPattern="/SystemNoticeInfoService")
public class SystemNoticeInfoService {
	String xmlFile="notice.xml";
	public SystemNoticeInfo readNotice() throws Exception{
		
		SystemNoticeInfo noticeInfo=new SystemNoticeInfo();
		// 很明显该类是一个单例,先获取产生DocumentBuilder工厂    
		// 的工厂,在通过这个工厂产生一个DocumentBuilder,    
		// DocumentBuilder就是用来产生Document的    
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
		dbf.setIgnoringElementContentWhitespace(true);
		DocumentBuilder db = dbf.newDocumentBuilder();    
		// 这个Document就是一个XML文件在内存中的镜像    
		Document doc = db.parse(new File(xmlFile)); 
		doc.normalize(); 
		Element element = doc.getDocumentElement(); //取出根元素
		NodeList cfgs = element.getElementsByTagName("config");
		System.out.println(1);
		for(int j=0;j<cfgs.getLength();j++){
			Element child=(Element)cfgs.item(j);
			
			NodeList year=child.getElementsByTagName("title");
			Element e=(Element)year.item(0);
			Node t=e.getFirstChild();
			noticeInfo.setTitle(t.getNodeValue());
			
			NodeList season=child.getElementsByTagName("publishTime");
			e=(Element)season.item(0);
			t=e.getFirstChild();
			noticeInfo.setPublishTime(t.getNodeValue());
			
			NodeList startTime=child.getElementsByTagName("content");
			e=(Element)startTime.item(0);
			t=e.getFirstChild();
			noticeInfo.setContent(t.getNodeValue());
			}
		System.out.println(1111);
		return noticeInfo;

	}
	@Interceptors(ProfileInterceptor.class)
	public String updateNotice(@WebParam(name = "uc") UserCave uc,
			@WebParam(name="sni")SystemNoticeInfo sni) throws Exception{
		SystemNoticeInfo noticeInfo=new SystemNoticeInfo();
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
		dbf.setIgnoringElementContentWhitespace(true);
		DocumentBuilder db = dbf.newDocumentBuilder();    
		// 这个Document就是一个XML文件在内存中的镜像    
		Document doc = db.parse(new File(xmlFile)); 
		doc.normalize(); 
		Element element = doc.getDocumentElement(); //取出根元素
		NodeList cfgs = element.getElementsByTagName("config");
		
		for(int j=0;j<cfgs.getLength();j++){
			Element child=(Element)cfgs.item(j);
			//获取标题
			NodeList year=child.getElementsByTagName("title");
			Element e=(Element)year.item(0);
			Node t=e.getFirstChild();
			t.setNodeValue(sni.getTitle());
			
			//获取发表时间
			NodeList season=child.getElementsByTagName("publishTime");
			e=(Element)season.item(0);
			t=e.getFirstChild();
			t.setNodeValue(sni.getPublishTime());
	
			//获取内容
			NodeList startTime=child.getElementsByTagName("content");
			e=(Element)startTime.item(0);
			t=e.getFirstChild();
			t.setNodeValue(sni.getContent());
			

		}
       doc2XmlFile(doc,xmlFile); 
		return "修改公告成功";
	}
	private static boolean doc2XmlFile(Document document,String filename)    
    {    
      boolean flag = true;    
      try    
       {    
            /** 将document中的内容写入文件中   */    
             TransformerFactory tFactory = TransformerFactory.newInstance();       
             Transformer transformer = tFactory.newTransformer();     
            /** 编码 */    
            transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");    
             DOMSource source = new DOMSource(document);     
             StreamResult result = new StreamResult(new File(filename));  
             transformer.transform(source, result);     
         }catch(Exception ex)    
         {    
             flag = false;    
             ex.printStackTrace();    
         }    
        return flag;          
    } 
}
